package com.qqonline.conmon;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class getUrlResponse {
	public static String getUtfString(String url) throws Exception{
		HttpClient client = new DefaultHttpClient();  
		//HttpPost post = new HttpPost(url);
        HttpGet get = new HttpGet(url);  
        HttpResponse response = client.execute(get); 
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
            HttpEntity entity = response.getEntity();  
            InputStream is = entity.getContent(); 
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            byte[] buf = new byte[1024];
            int length = -1;  
            while ((length = is.read(buf)) != -1) {  
                baos.write(buf, 0, length);
            }
            return new String(baos.toByteArray(),"utf-8");
        }
    	return null;
    }
	public static String postUtfString(String url) throws Exception{
		HttpClient client = new DefaultHttpClient();  
		HttpPost post = new HttpPost(url);
        HttpResponse response = client.execute(post); 
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
            HttpEntity entity = response.getEntity();  
            InputStream is = entity.getContent(); 
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            byte[] buf = new byte[1024];
            int length = -1;  
            while ((length = is.read(buf)) != -1) {  
                baos.write(buf, 0, length);
            }
            return new String(baos.toByteArray(),"utf-8");
        }
    	return null;
    }
}
